package com.keen.gzr.androidjs;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * 描述:自定义WebView
 */

public class CustomWebView extends WebView {

    private static final String TAG = "MyWebView";
//    private ProgressBar progressbar;
    public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2;
    private Context mContext;

    public CustomWebView(Context context) {
        super(context);
        mContext = context;



    }

    @SuppressLint("SetJavaScriptEnabled") @SuppressWarnings("deprecation")
    public CustomWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
//        progressbar = new ProgressBar(context, null,
//                android.R.attr.progressBarStyleHorizontal);
//        progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
//                10, 0, 0));
//        addView(progressbar);
        setWebViewClient(new MyWebViewClient());
//        setWebChromeClient(new WebChromeClient());
        WebSettings webSettings = getSettings();
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAppCacheEnabled(true);
        String appCachePath = context.getCacheDir().getAbsolutePath();
        Log.v("zr", "appCachePath----------->" + appCachePath);
        webSettings.setAppCachePath(appCachePath);
        webSettings.setAllowFileAccess(true);// 设置允许访问文件数据
        webSettings.setJavaScriptEnabled(true);
        //--------------屏幕自适应-----------
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        setScrollbarFadingEnabled(false);
        //------------------
    }

    public class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.v(TAG, "shouldOverrideUrlLoading( )" + url);
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            String data = "Page NO FOUND！";
            view.loadUrl("javascript:document.body.innerHTML=\"" + data + "\"");
        }
    }

    public class WebChromeClient extends android.webkit.WebChromeClient {

//        @Override
//        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
//                                         FileChooserParams fileChooserParams) {
//            Log.v("zr" , "onShowFileChooser--------->");
////            openFileChooserImplForAndroid5(filePathCallback);
//            return true;
//        }

//        @Override
//        public void onProgressChanged(WebView view, int newProgress) {
//            super.onProgressChanged(view, newProgress);
//            if (newProgress == 100) {
//                progressbar.setVisibility(GONE);
//            } else {
//                if (progressbar.getVisibility() == GONE)
//                    progressbar.setVisibility(VISIBLE);
//                progressbar.setProgress(newProgress);
//            }
//
//        }
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
//        LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
//        lp.x = l;
//        lp.y = t;
//        progressbar.setLayoutParams(lp);
        super.onScrollChanged(l, t, oldl, oldt);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && this.canGoBack()) {
            this.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }
}
